import { getToken } from '@/utils/local';

const initialState = {
  profile: {} as Profile,
  auth: getToken(),
  user: {} as User,
};
export type TokenType = {
  token: string;
  refresh_token: string;
};
export type User = {
  id: string;
  name: string;
  photo: string;
  art_count: number;
  follow_count: number;
  fans_count: number;
  like_count: number;
};
export type Profile = {
  id: string;
  photo: string;
  name: string;
  mobile: string;
  gender: number;
  birthday: string;
  intro: string | null;
};
export type UserAtion =
  | { type: 'login/token'; payload: TokenType }
  | { type: 'user/getUserProfile'; payload: Profile }
  | { type: 'user/editFile'; payload: Partial<Profile> }
  | { type: 'user/getProfile'; payload: User };
export default function userReducer(state = initialState, action: UserAtion) {
  switch (action.type) {
    case 'login/token':
      return {
        ...state,
        auth: action.payload,
      };
    case 'user/getProfile':
      return {
        ...state,
        user: action.payload,
      };
    case 'user/getUserProfile':
      return {
        ...state,
        profile: action.payload,
      };
    case 'user/editFile':
      return {
        ...state,
        profile: { ...state.profile, ...action.payload },
      };

    default:
      return state;
  }
}
